package view;



import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * This class renders the edit template view which can also be used to create new view.
 * 
 * The edit template panel contains buttons for add/ cancel/ update operations, text field for the title
 * and text area for the body.
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class EditTemplatePanel extends JPanel {
	
	/**
	 * The buttons panel
	 */
	private JPanel buttonsPanel;

	/**
	 * Okay button
	 */
	private JButton addBtn, cancelBtn, updateBtn;
	
	/**
	
	 * @return the editBtn */
	public JButton getEditBtn() {
		return updateBtn;
	}

	/**
	 * Title field
	 */
	private JTextField title;
	
	/**
	 * Body field
	 */
	private JTextArea body;

	/**
	 * Constructor
	 */
	public EditTemplatePanel(){
		
		super(new MigLayout("fillx"));
		initComponents();
		setup();
	}
	
	/**
	 * Sets the current mode, that is edit or create. This inturn hides the appropriate buttons
	 * @param mode "create" or "update"
	
	 */
	public void setMode(String mode){
		
		if (mode.equals("create")){
			buttonsPanel.remove(updateBtn);
			return;
		}
		if (mode.equals("update")){
			title.setEditable(false);
			buttonsPanel.remove(addBtn);
			return;
		}
	}

	/**
	 * Sets up the positioning in the panel
	 */
	private void setup() {
		
		this.add(new JLabel("Title: "));
		this.add(title, "width 400, wrap");
		this.add(new JLabel("Body: "));
		this.add(body, "width 400, height 300, wrap");
		
		buttonsPanel = new JPanel(new MigLayout("fillx"));
		this.add(buttonsPanel, "skip 1, growx");
		
		buttonsPanel.add(addBtn, "align right, push");
		buttonsPanel.add(updateBtn, "align right, push");
		buttonsPanel.add(cancelBtn, "align right");

	}

	/**
	
	 * @return the cancelBtn */
	public JButton getCancelBtn() {
		return cancelBtn;
	}


	/**
	 * Initializes the components
	 */
	private void initComponents() {
		
		addBtn = new JButton("Add", IconsManager.getImageIcon("small/plus.png"));
		updateBtn = new JButton("Update", IconsManager.getImageIcon("small/tick.png"));
		cancelBtn = new JButton("Cancel", IconsManager.getImageIcon("small/cross.png"));
		title = new JTextField();
		body = new JTextArea();
	}

	/**
	
	 * @return the addBtn */
	public JButton getAddBtn() {
		return addBtn;
	}

	/**
	
	 * @return the title */
	public JTextField getTitle() {
		return title;
	}

	/**
	
	 * @return the body */
	public JTextArea getBody() {
		return body;
	}
	
}